<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/1/31 0031
 * Time: 下午 16:11
 */

namespace app\admin\model;

use think\Model;
use think\Db;
use think\Cache;
class Order extends  Model
{
    protected  $pk='order_id';//主键
    protected $table = 'xcx_order';
    public function getAllOrderData()
    {
       /* $res=$this->order('order_id desc ')->select();
        foreach ($res as $k=>$v)
        {
            $v['arr_snap_items']=json_decode($v['snap_items'],true);
            $v['arr_snap_address']=json_decode($v['snap_address'],true);
            $res[$k]=$v;
        }*/
        $res=Db::table('xcx_order')
            ->alias('a')
            ->join('order_logistics o','a.order_id=o.order_id','LEFT')
            ->order('a.order_id desc')
            ->field('a.order_id,o.logistics_json,a.order_number,a.order_states,a.total_price,a.snap_name,a.snap_img,a.total_count,a.snap_items,a.snap_address,o.product_id,o.logistics_number,o.logistics_state,o.logistics_json,a.createtime')
            ->limit(10)
            ->select();
        //halt($res);
        $wuliucompang=[];
        //先查看物流是否收到货要是没有去查接口
        //要是用直接展示信息
        foreach ($res as $k=>$v)
        {
            if($v['logistics_number']) {
                if ($v['logistics_state'] < 4) {
                    //去查询接口

                    $logistics_json = $this->getWuLiu($v['logistics_number']);
                    $wuLiuInfo = json_decode($logistics_json, true);
                    if($wuLiuInfo['result'])
                    {
                        if (intval($wuLiuInfo['result']['deliverystatus']) >= 3) {
                            //去更新物流数据
                            $this->postWuliu($v['logistics_number'], $wuLiuInfo);
                        };
                    }

                } else {
                    $logistics_json = $v['logistics_json'];
                }
                $v['arr_logistics_json'] = json_decode($logistics_json, true);
                $compang = json_decode(Cache::get('wuliuCompang'), true)['result'];

                if(isset($v['arr_logistics_json']['result']['type'])){
                    for ($i = 0; $i < count($compang); $i++) {
                        if ($v['arr_logistics_json']['result']['type'] == strtolower($compang[$i]['type'])) {
                            $wuliucompang = $compang[$i];
                        }
                    }
                }

            }
            $productId=explode(',',$v['product_id']);
            $v['place']=db('product_place')->where('product_id',$productId[0])->find();
           $v['compang']=$wuliucompang;
            $v['arr_snap_items']=json_decode($v['snap_items'],true);
            $v['arr_snap_address']=json_decode($v['snap_address'],true);
            foreach ($v['arr_snap_items'] as $kk=>$vv)
            {
                $vv['speci_snap']=json_decode($vv['speci_snap'], true);
                $v['arr_snap_items'][$kk]=$vv;
            }
            $res[$k]=$v;
        }

        return $res;
    }
    public function postWuliu($number,$wuLiuInfo)
    {
        $arr['logistics_json']=json_encode($wuLiuInfo);
        $arr['logistics_state']=4;
        $arr['update_time']=time();
        db('order_logistics')->where('logistics_number',$number)->update($arr);
    }
    public function getWuLiu($number)
    {
        $appcode='bd2f0644074f4ffab2ddb4d5c6ffef46';
        $res=wuliu($appcode,$number);
        $string="{\"status";
        $res=(explode($string,$res));
        $res=$string.$res[1];
        return $res;
    }
    public function addLogisticsInfo($data)
    {
            $arr['product_id']=$data['product_id'];
            $arr['logistics_number']=$data['logistics_number'];
            $arr['logistics_state']=$data['logistics_state'];
            $arr['createtime']=time();
            $result=db('order_logistics')->save($arr);
            if(false === $result){
                // 验证失败 输出错误信息
                return ['valid'=>1,'msg'=>'添加失败'];
            }else{
                //添加操作记录
                return ['valid'=>4,'msg'=>'添加成功'];
            }
    }
    public function upodate_log($data)
    {
            //获得商品总件数/和图片
            $orderInfo=$this->where('order_id',$data['id'])->value('snap_items');
            $snap_items=json_decode($orderInfo,true);

            $products_data=ltrim($data['sps_id'],',');
        foreach ($snap_items as $k=>$v)
        {
            $v['yuan_pro']=$v['id'].'-'.$v['speci_id'];
            $snap_items[$k]=$v;
        }
        $getData=[];
        $count=explode(",",$products_data);

        for($i=0;$i<count($count);$i++)
        {
            for($j=0;$j<count($snap_items);$j++)
            {
                if($count[$i]==$snap_items[$j]['yuan_pro'])
                {
                    $getData[$i]=$snap_items[$j];
                }
            }
        }

            $arr_counts=0;
            $arr_img='';
            for($i=0;$i<count($getData);$i++)
            {
                $arr_counts +=$getData[$i]['counts'];
                $img_arr['createtime']=time();
                $img_arr['eimg_url']=$getData[$i]['main_img_url'];
                $img_id=db('logis_img')->insertGetId($img_arr);
                $arr_img=$arr_img+$img_id.',';
            }
            $arr['order_id']=$data['id'];
            $arr['product_id']=$products_data;
            $arr['logistics_number']=$data['value'];
            $arr['counts']=$arr_counts;
            $arr['img_arr']=rtrim($arr_img,',');
            $arr['createtime']=time();
            $result=db('order_logistics')->insertGetId($arr);

            if(false === $result){
                // 验证失败 输出错误信息
                return ['valid'=>1,'msg'=>'添加失败'];
            }else{
                //添加操作记录

                $this->where('order_id',$data['id'])->update(['order_states'=>3]);
                $this->where('order_id',$data['id'])->setInc('evaluate_arr',1);
                return ['valid'=>4,'msg'=>'添加成功'];
            }
    }
}